<?php if ( ! defined('BASEPATH')) exit('This file does not run directly. Sorry.'); ?>
<?php
/**
 * Kalaisoo.
 *
 * A lightweight no-framework framework written in PHP.
 *
 * @package Kalaisoo
 * @author $Author: info@sah-company.com $
 * @version $Id: addressbook.php 151 2012-02-01 01:08:39Z info@sah-company.com $
 */

/**
 * Load dependencies.
 */
Registry::instance()->loader->model(array(
	'person',
	'email',
	'phone',
	'url',
	'address',
	'related_person'
	)
);

/**
 * Kalaisoo Addressbook Model Class.
 *
 * @package Kalaisoo
 * @subpackage Model
 * @author $Author: info@sah-company.com $
 */
class Model_Addressbook extends Model
{
	/**
	 * default order field and direction
	 *
	 * @var array
	 */
	public $order = array(
		'field' => 'person_last_name',
		'dir' => 'ASC'
	);

	/**
	 * __construct.
	 */
	public function construct()
	{
		parent::__construct();
	}
	
	/**
	 * returns the default order clause for sql queries
	 *
	 * @return array
	 */
	public function orderClause()
	{
		return 'person_last_name ASC, person_first_name ASC';
		//return $this->order['field'].' '.$this->order['dir'];
	}
	
	/**
	 * returns the tags of a bean.
	 *
	 * @return array
	 */
	public function getTags() {
		return array(
			$this->bean->person->nickname,
			$this->bean->person->first_name,
			$this->bean->person->last_name,
			$this->bean->person->organization,
			$this->bean->person->maiden_name,
			$this->bean->person->middle_name
		);
	}
	
	/**
	 * returns the url or src to the addressbook avatar image.
	 */
	public function gravatar()
	{
		return $this->gravatarWorkhorse($this->bean->person->image);
	}
	
	/**
	 * returns the path to a avatar image
	 *
	 * @param string $image
	 */
	public static function gravatarWorkhorse($image = null)
	{
		$pathToImage = ($image === null) ? 'user.png' : $image;
		return ahref(Registry::instance()->config['uploads']['path'].'/'.$pathToImage);
	}
	
	/**
	 * returns the birthdate, if given or false
	 *
	 * @return mixed
	 */
	public function birth_date()
	{
		if ($this->bean->person->birth_date == '0000-00-00') return false;
		return $this->bean->person->birth_date;
	}
	
	/**
	 * returns an array with special actions the model can take.
	 *
	 * @return array
	 */
	public function actions()
	{
		if ( ! $this->bean->getId()) return array();
		return array(
			'itsme' => true
		);
	}
	
	/**
	 * relates this addressbook entry to the current user.
	 *
	 */
	public function itsme()
	{
		try {
			$this->controller()->user()->addressbook = $this->bean;
			R::store($this->controller()->user());
			$this->controller->data['messages'][] = __('addressbook_itsme_success');
			$this->controller()->user()->autoBlog($this->bean, 'itsme');
		} catch (Exception $e) {
			$this->controller->data['errors'][] = __('addressbook_itsme_failed');
		}
		return $this->controller()->view($this->bean->getMeta('type'), $this->bean->getId());
	}
	
	/**
	 * loads the array $this->gestalt of this bean for use with Model::gestalt().
	 */
	public function loadGestalten()
	{
		$this->gestalt['default'] = array(
			array(
				'type' => 'nested',
				'name' => 'person'
			),
			array(
				'type' => 'own',
				'name' => 'email',
				'parameters' => array(
					'sort' => array(
						'name' => 'value',
						'direction' => 'asc'
					)
				)
			),
			array(
				'type' => 'own',
				'name' => 'phone',
				'parameters' => array(
					'sort' => array(
						'name' => 'value',
						'direction' => 'asc'
					)
				)
			),
			array(
				'type' => 'own',
				'name' => 'url',
				'parameters' => array(
					'sort' => array(
						'name' => 'value',
						'direction' => 'asc'
					)
				)
			),
			array(
				'type' => 'own',
				'name' => 'address',
				'parameters' => array(
					'sort' => array(
						'name' => 'city',
						'direction' => 'asc'
					)
				)
			)
		);
	}
	
	/**
	 * validates the data and returns either true or false.
	 *
	 * @return bool
	 */
	public function validate()
	{
		$valid = true;
		return $valid;
	}
	
	/**
	 * open.
	 */
	public function open()
	{
	}

	/**
	 * dispense.
	 */
	public function dispense()
	{
	}

	/**
	 * update.
	 */
	public function update()
	{
	}
	
	/**
	 * after_update.
	 */
	public function after_update()
	{
	}
	
	/**
	 * delete.
	 */
	public function delete()
	{
	}
	
	/**
	 * after_delete.
	 */
	public function after_delete()
	{
	}
}
?>